package com.shop.cereshop.admin.controller.userAuth;
import com.shop.cereshop.admin.annotation.NoRepeatSubmit;
import com.shop.cereshop.admin.annotation.NoRepeatWebLog;
import com.shop.cereshop.admin.aspect.OperationLog;
import com.shop.cereshop.admin.dao.buyer.UserStopSpeeckDAO;
import com.shop.cereshop.admin.page.user.BuyerUsersVO;
import com.shop.cereshop.admin.page.wallet.TransactionVO;
import com.shop.cereshop.admin.param.buyer.*;
import com.shop.cereshop.admin.redis.service.api.impl.UserRedisServiceImpl;
import com.shop.cereshop.admin.service.buyer.CereBuyerUserService;
import com.shop.cereshop.admin.service.buyer.UserStopSpeeckService;
import com.shop.cereshop.admin.service.order.CereShopOrderService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.buyer.UserStopSpeeck;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.history.CereWebHistory;
import com.shop.cereshop.commons.domain.im.tx.req.user.SetGlobalBanReq;
import com.shop.cereshop.commons.domain.im.tx.resp.ImBaseResp;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.service.im.ImUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
@Slf4j
@RestController
@RequestMapping("/userManage")
@Api(value = "UserManage", tags = "用户管理")
public class UserManageController {

    @Autowired
    ImUserService imUserService;

    @Autowired
    CereBuyerUserService cereBuyerUserService;

    @Autowired
    UserStopSpeeckService userStopSpeeckService;

    @Autowired
    UserStopSpeeckDAO userStopSpeeckDAO;

    @Autowired
    CereShopOrderService cereShopOrderService;

   @Autowired
   UserRedisServiceImpl userRedisService;




    @PostMapping("/updateOther")
    @ApiOperation(value = "禁用或启用用户功能(点赞,收藏,评论等)")
    public Result<Void> updateOther(@RequestBody @NotNull(message = "参数不能为空") BuyerOtherParam param,HttpServletRequest request) throws CoBusinessException {
        Integer number = cereBuyerUserService.updateOther(param,request);
        if (number > 0){
            return new Result<>(CoReturnFormat.SUCCESS);
        }
        return new Result<>(CoReturnFormat.FAIL);
    }

    @PostMapping("/update/im")
    @ApiOperation(value = "修改用户IM状态")
    public Result<Void> updateIm(@RequestBody @NotNull(message = "参数不能为空") BuyerImParam param,HttpServletRequest request) throws CoBusinessException {
        Integer number = cereBuyerUserService.updateIm(param,request);
        if (number > 0){
            return new Result<>(CoReturnFormat.SUCCESS);
        }

        return new Result<>(CoReturnFormat.FAIL);
    }



    @PostMapping("/update/trends")
    @ApiOperation(value = "修改用户动态发布的状态")
    public Result<Void> updateTrends(@RequestBody @NotNull(message = "参数不能为空") BuyerTrendsParam param,HttpServletRequest request) throws CoBusinessException {
        Integer number = cereBuyerUserService.updateTrends(param,request);
        if (number > 0){
            return new Result<>(CoReturnFormat.SUCCESS);
        }

        return new Result<>(CoReturnFormat.FAIL);
    }



    @PostMapping("/stop/speck/delete")
    @ApiModelProperty(value = "解除用户禁言")
    public Result<Void> deleteUserStopSpeck(@RequestBody @NotNull(message = "参数不能为空") UserManageParam userManageParam, HttpServletRequest request) throws CoBusinessException {
        CerePlatformUser user = (CerePlatformUser)request.getAttribute("user");
        if (user == null) {
            throw new CoBusinessException(CoReturnFormat.USER_NOT_EXIST);
        }
        UserStopSpeeck userStopSpeeck = userStopSpeeckDAO.selectByUserId(Long.valueOf(userManageParam.getAccount()));
        if (userStopSpeeck != null){
            Integer  number=  userStopSpeeckService.spanSpeech(userManageParam, request);
            if (number > 0){
                return new Result<>(CoReturnFormat.SUCCESS);
            }
            return new Result<>(CoReturnFormat.FAIL);
        }
        return new Result<>(CoReturnFormat.USER_NOT_EXIST_OR_DELETED);

    }



    @PostMapping("/buyerUser/update")
    @ApiOperation(value = "用户封禁状态更改")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @OperationLog(
            operation = "禁用了id为{buyerUserId}的用户",
            operationType = "改"
    )
    public Result<Void> updateAccount(@RequestBody @NotNull(message = "参数不能为空") BuyerGetUsersParam param,HttpServletRequest request) throws CoBusinessException {
        // 获取当前登录用户
        CerePlatformUser currentUser = (CerePlatformUser) request.getAttribute("user");
        if (currentUser == null) {
            throw new CoBusinessException(CoReturnFormat.USER_NOT_EXIST);
        }
        if (param.getState() ==0){
            userRedisService.clearAllTokens(param.getBuyerUserId());
        }
        cereBuyerUserService.updateAccount(param,request);

        return Result.success();
    }


//    @GetMapping("/getUserInfo")
//    @ApiModelProperty(value = "获取用户信息")
//    public Result <Page<CereBuyerUser>> getUserInfo(PageParam pageParam, HttpServletRequest request) throws CoBusinessException {
//        //获取当前登录账户
//        CerePlatformUser user = ( CerePlatformUser)request.getAttribute("user");
//        if (user == null){
//            throw new CoBusinessException(CoReturnFormat.USER_NOT_EXIST);
//        }
//        if (pageParam.getPage() == null || pageParam.getPageSize() == null){
//            pageParam.setPage(1);
//            pageParam.setPageSize(10);
//        }
//        Page<CereBuyerUser> page  =cereBuyerUserService.selectUserInfo(pageParam);
//
//        return  new Result<>(page, CoReturnFormat.SUCCESS);
//    }




    @PostMapping("/stop/speck")
    @ApiModelProperty(value = "禁言用户")
    public Result<Void> stopSpeck( @RequestBody UserManageParam userManageParam, HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CerePlatformUser user = (CerePlatformUser)request.getAttribute("user");
        if (user == null) {
//            用户不存在
            throw new CoBusinessException(CoReturnFormat.USER_NOT_EXIST);
        }
        if (userManageParam.getStopSpeeckType()==1 && userManageParam.getAccount() != null && !(userManageParam.getAccount().equals(""))) {

            SetGlobalBanReq setGlobalBanReq  = new SetGlobalBanReq();
            setGlobalBanReq.setSetAccount(userManageParam.getAccount());
//            判断是否传递相对应的参数
            if (userManageParam.getC2CmsgNospeakingTime() != null&& userManageParam.getC2CmsgNospeakingTime() > 0){
                setGlobalBanReq.setC2CmsgNospeakingTime(userManageParam.getC2CmsgNospeakingTime());
            }
            if (userManageParam.getGroupmsgNospeakingTime() != null && userManageParam.getGroupmsgNospeakingTime() > 0){
                setGlobalBanReq.setGroupmsgNospeakingTime(userManageParam.getGroupmsgNospeakingTime());
            }
            ImBaseResp imBaseResp = imUserService.setGlobalBan(setGlobalBanReq);
            if (imBaseResp.getErrorCode() == 0) {
//                操作禁言状态表，同步数据到用户禁言状态表
                Integer number = userStopSpeeckService.userStopSpeeck(userManageParam,request);
                if (number > 0){
                    return new Result<>(CoReturnFormat.SUCCESS);
                }else {
                    return new Result<>(CoReturnFormat.SYNC_ERROR);
                }
            }

        }
        return new Result<>(CoReturnFormat.FAIL);
    }




    /**
     * 用户列表查询接口
     */
    @GetMapping("/getUserList")
    @ApiOperation(value = "用户列表查询")
    public Result<Page<BuyerUsersVO>> getUserList(@ModelAttribute @NotNull(message = "参数不能为空") BuyerGetUsersParam param) {
        Page<BuyerUsersVO> result = cereBuyerUserService.getAllUser(param);
        return Result.success(result);
    }

    /** todo
     * 统一查询用户收获地址的接口
     */
    @GetMapping("/getUserAddress")
    @ApiOperation(value = "统一查询用户收获地址")
    public Result<Page<String>> getUserAddress(@ModelAttribute @NotNull(message = "参数不能为空") BuyerGetUsersParam param) {
        Page<String> result = cereShopOrderService.getUserAddress(param);
        return Result.success(result);
    }



    /**
     * 用户流水查询(用户所有流水)
     */
    @GetMapping("/transaction/get")
    @ApiOperation(value = "用户流水查询")
    public Result<TransactionVO> getTransaction(@ModelAttribute @NotNull(message = "参数不能为空") BuyerGetUsersParam  param) {
        TransactionVO result = cereBuyerUserService.getTransaction(param);
        return Result.success(result);
    }

    /**
     * 用户详情查询接口
     */
    @GetMapping("/detail/get")
    @ApiOperation(value = "用户详情查询")
    public Result<CereBuyerUser> getUserDetail(@ModelAttribute @NotNull(message = "参数不能为空") BuyerGetUsersParam param) {
        CereBuyerUser result = cereBuyerUserService.getByUserId(param.getBuyerUserId());
        return Result.success(result);
    }
    /**
     * 用户基本信息查询
     */
    @PostMapping("/getUserInfo")
    @ApiOperation(value = "获取用户基本信息")
    public Result<Page<CereBuyerUser>> getUserInfo(@RequestBody BuyerBaseUserInfo buyerBaseUserInfo , HttpServletRequest request) throws CoBusinessException {
        CerePlatformUser user = (CerePlatformUser)request.getAttribute("user");
        if (user == null){
            throw new CoBusinessException(CoReturnFormat.USER_NOT_EXIST);
        }
        Page<CereBuyerUser> userBaseeInfo = cereBuyerUserService.getUserBaseeInfo(buyerBaseUserInfo);

        return Result.success(userBaseeInfo);
    }

    @PostMapping("/updateUserBaseInfo")
    @ApiOperation(value = "用户基本信息修改")
    public Result<Void> updateUserBaseInfo(@RequestBody @NotNull(message = "参数不能为空") CereBuyerUser param,HttpServletRequest request) throws CoBusinessException {
        Integer number = cereBuyerUserService.updateUserBaseInfo(param, request);
        if (number > 0){
            return new Result<>(CoReturnFormat.SUCCESS);
        }

        return new Result<>(CoReturnFormat.FAIL);
    }






}
